################################################################################
# Judicial Ideology as Text -- Figure A1 (Static Wordshoal Data Summary)
# Truscott & Romano
# Updated February 2025
################################################################################

################################################################################
# Necessary Packages & Dependencies
################################################################################

source('R/truscott_romano_source.R')

################################################################################
#Figure A1: Static Wordshoal Summary by Term
################################################################################

wordshoal_static_summaries <- data.frame()

for (weight in 1:length(wordshoals_static)){

  wordshoal_static_summaries <- bind_rows(wordshoal_static_summaries, wordshoals_static[[1]]$psi_doc)

}

wordshoal_static_summaries <- wordshoal_static_summaries %>%
  distinct(opinion_id) %>%
  left_join(decisions, by = 'opinion_id', relationship = 'many-to-many') %>%
  distinct(opinion_id, .keep_all = T) %>%
  dplyr::select(docket, opinion_id, term, author_last, issue_area, minVotes, vote) %>%
  rename(decision_type = vote)

{

  Figure_A1 <- wordshoal_static_summaries %>%
    mutate(decision_type = case_when(
      .default = '(Special) Concurrence\n(In Judgement)',
      decision_type == 'Majority' ~ 'Majority',
      decision_type %in% c('Dissent', 'Jurisdictional Dissent') ~ '(Jurisdictional)\nDissent'),
      decision_type = factor(decision_type, levels = c('Majority', '(Special) Concurrence\n(In Judgement)', '(Jurisdictional)\nDissent'))) %>%
    group_by(term, decision_type) %>%
    summarise(count = n()) %>%
    ungroup() %>%
    ggplot(aes(x = as.numeric(term), y = count)) +
    geom_col(aes(fill = decision_type), colour = 'black', position = position_stack(reverse = T)) +
    geom_label(aes(label = count, group = decision_type),
               position = position_stack(vjust = 0.5, reverse = T)) +
    scale_y_continuous(breaks = seq(25, 150, 25)) +
    geom_hline(yintercept = 0, colour = 'black') +
    scale_fill_manual(values = c('deepskyblue3', 'cornsilk', 'coral3')) +
    labs(x = '\nTerm',
         y = 'Count\n') +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 14, colour = 'black'),
          axis.title = element_text(size = 16, colour = 'black'),
          strip.background = element_rect(size = 1, colour = 'black', fill = 'grey'),
          strip.text = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.title = element_blank(),
          legend.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.text = element_text(size = 14, colour = 'black', hjust = 0.5))


  ggsave(Figure_A1,
         file = 'figures/Figure_A1.png',
         width = 10,
         height = 8,
         units = 'in',
         bg = 'white')

  ggsave('figures_tiff/Figure_A1.tiff',
         plot = Figure_A1,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

  } # Figure A1: Opinions by Term


